package com.ytjj.common.utils;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipFileUtil {
    public static void main(String[] args) {
        readInnerZipFile(new File("F:\\1639124655065.zip"), "F:\\bcd.zip");
    }

    public static void readInnerZipFile(File zipFile, String innerZipFileEntryName) {
        ZipFile outerZipFile = null;

        File tempFile = null;

        FileOutputStream tempOut = null;

        ZipFile innerZipFile = null;

        try {
            outerZipFile = new ZipFile(zipFile);
            tempFile = File.createTempFile("tempFile", "zip");
            tempOut = new FileOutputStream(tempFile);

            IOUtils.copy(outerZipFile.getInputStream(new ZipEntry(innerZipFileEntryName)), tempOut);

            innerZipFile = new ZipFile(tempFile);

            Enumeration<? extends ZipEntry> entries = innerZipFile.entries();

            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();

                System.out.println(entry);


            }

        } catch (IOException e) {
            e.printStackTrace();

        } finally {

            try {
                if (outerZipFile != null)
                    outerZipFile.close();

            } catch (IOException e) {
                e.printStackTrace();

            }

            IOUtils.closeQuietly(tempOut);

            if (tempFile != null && !tempFile.delete()) {
                System.out.println("Could not delete " + tempFile);

            }

            try {
                if (innerZipFile != null)

                    innerZipFile.close();

            } catch (IOException e) {
                e.printStackTrace();

            }

        }

    }
}
